package cn.ctyun.player.activity;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;

import cn.ctyun.player.R;

public class MainActivity extends AppCompatActivity {

    private static final String URL_VOD = "http://help.qhoss.xstore.ctyun.cn/video/1080-1M.flv";
    private static final String URL_M3U8 = "http://ivi.bupt.edu.cn/hls/cctv6.m3u8";
    private EditText editPlayUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editPlayUrl = findViewById(R.id.edit_play_url);
        editPlayUrl.setText(URL_VOD);

        // 6.0系统请求摄像头权限
        if (Build.VERSION.SDK_INT > 22){
            List<String> permissionList = new ArrayList<>();
            if (ContextCompat.checkSelfPermission(MainActivity.this,
                    android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
                permissionList.add(android.Manifest.permission.CAMERA);
            }
            if (ContextCompat.checkSelfPermission(MainActivity.this,
                    android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
                permissionList.add(android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
            }
            if (!permissionList.isEmpty()) {
                ActivityCompat.requestPermissions(MainActivity.this,
                        permissionList.toArray(new String[permissionList.size()]), 1);
            }
        }
    }

    public void playerDemo(View view) {
        Intent intent = new Intent(MainActivity.this, DemoActivity.class);
        intent.putExtra("url", editPlayUrl.getText().toString());
        startActivity(intent);
    }

    public void scanQrcode(View view) {
        startActivityForResult(new Intent(this, QrcodeScanActivity.class), 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1 && resultCode == 1){
            Bundle bundle = data.getExtras();
            String url = bundle.getString("url");
            editPlayUrl.setText(url);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
